#include <Arduino.h>
#include "led_control.h"
#include "wifi_connection.h"
#include "web_server.h"
#include "msg_queue.h"
#include "ir_control.h"
#include "serial_print.h"

// Define LED pin
const int ledPin = 12;
// 红外发送引脚
const int irSendPin = 13;
// 红外接收引脚
const int irReceivePin = 14;

static MessageQueue _queue;
static webserver_cabllback ws_cb;
static wifi_cabllback wc_cb;

extern wifi_opt_t wifi_opt;
extern webserver_opt_t webserver_opt;
extern led_control_opt_t led_control_opt;
extern ir_control_opt_t ir_control_opt;
extern serial_print_opt_t serial_print_opt;

static void on_wifi_connect_cb(int status) {
    switch (status) {
        case 0: // Connecting
            static bool led_status = false;
            serial_print_opt.println_string("正在连接 Wi-Fi...");
            led_status ? led_control_opt.turn_on(ledPin): led_control_opt.turn_off(ledPin);
            led_status = !led_status; // Toggle LED status
            break;
        case 1: // Connected
            serial_print_opt.println_string("Wi-Fi 连接成功！");
            led_control_opt.turn_on(ledPin);
            break;
        case 2: // Disconnected
            serial_print_opt.println_string("Wi-Fi 连接失败，请重试。");
            led_control_opt.turn_off(ledPin);
            break;
        default:
            serial_print_opt.println_string("未知的 Wi-Fi 状态");
            break;
    }
}
void on_wifi_mode_cb(int wifi_mode, wifi_info* wi) {
    char buffer[256];
    led_control_opt.turn_on(ledPin); // Blink LED to indicate mode change
    if (wifi_mode == 0) {
        snprintf(buffer, sizeof(buffer), "Enter AP mode, SSID: %s, Password: %s, IP: %s", wi->ssid, wi->password, wi->ip);
        serial_print_opt.println_string(buffer);
    } else if (wifi_mode == 1) {
        snprintf(buffer, sizeof(buffer), "Connected to WiFi, SSID: %s, Password: %s, IP: %s", wi->ssid, wi->password, wi->ip);
        serial_print_opt.println_string(buffer);
    }
}

// Callback function implementation
void on_webserver_wifi_cb(const String& ssid, const String& password) {
    Message message;
    message.type = MESSAGE_TYPE_WIFI_CONFIG;
    memcpy(message.body.wifiConfig.ssid , ssid.c_str(), sizeof(message.body.wifiConfig.ssid));
    memcpy(message.body.wifiConfig.password , password.c_str(), sizeof(message.body.wifiConfig.password));
    serial_print_opt.printf("Received Wi-Fi config: SSID=%s, Password=%s\n", message.body.wifiConfig.ssid, message.body.wifiConfig.password);
    enqueue(_queue, message);
}

// Callback function implementation
void on_webserver_led_cb(int ledPin, bool state) {
    state ? led_control_opt.turn_on(ledPin): led_control_opt.turn_off(ledPin);
}


// 重置 Wi-Fi 配置的回调函数
void on_webserver_reset_cb() {
    wifi_opt.clear();
    serial_print_opt.println_string("Wi-Fi 配置已重置");
}

void setup() {
    serial_print_opt.init(115200);
    led_control_opt.init(ledPin, true);
    ir_control_opt.init(irSendPin, irReceivePin);
    initQueue(_queue);

    // Setup Web server with callback
    wc_cb.wc_cb = on_wifi_connect_cb;
    wc_cb.wm_cb = on_wifi_mode_cb;
    wifi_opt.setup(&wc_cb);

    // Setup Web server with callback
    ws_cb.wc_cb = on_webserver_wifi_cb;
    ws_cb.led_cb = on_webserver_led_cb;
    ws_cb.reset_cb = on_webserver_reset_cb; // 设置重置回调函数
    webserver_opt.init(&ws_cb);
}

void loop() {
    // Receive message from the message queue
    Message msg;
    if (dequeue(_queue, msg)) {
        switch (msg.type) {
            case MESSAGE_TYPE_WIFI_CONFIG:
                wifi_opt.connect(msg.body.wifiConfig.ssid, msg.body.wifiConfig.password); 
                break;
            default:
                serial_print_opt.println_string("Unknown message type received");
                break;
        }
    }

    webserver_opt.handle();
    delay(20);
}
